package it.polito.connectedcar.tcc.zone;

import it.polito.connectedcar.vehicle.VehicleZone;

/**
 * Token implementation representing a Token of a Zone.
 * 
 * @author Nicola Aresta
 * 
 */
public class ZoneToken implements Token {

	private byte[] token;

	public ZoneToken(byte[] token) {
		this.token = token;
	}

	@Override
	public byte[] getBytes() {
		return token;
	}

	@Override
	public boolean equals(Object obj) {
		byte[] tokenBytes = null;
		if (obj instanceof VehicleZone)
			tokenBytes = ((VehicleZone) obj).getZoneToken().getBytes();
		else if (obj instanceof TCCZone)
			tokenBytes = ((TCCZone) obj).getT().getBytes();
		else if ((obj instanceof String))
			tokenBytes = ((String) obj).getBytes();
		else if ((obj instanceof Token))
			tokenBytes = ((Token) obj).getBytes();
		else
			return false;

		if (tokenBytes.length != getBytes().length)
			return false;
		for (int i = 0; i < tokenBytes.length; i++) {
			if (tokenBytes[i] != getBytes()[i])
				return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return new String(token);
	}
}
